//
//  SEGSettingViewModel.m
//  SEGViewModel
//
//  Created by 山刘 on 2020/6/5.
//

#import "SEGSettingViewModel.h"
#import <UMCommon/MobClick.h>

@implementation SEGSettingViewModel
#pragma mark - init
-(instancetype)init{
    if (self = [super init]) {
        self.switchStatus = [self segInitOpenSoundsStatus];
        [self racInit];
    }
    return self;
}
- (void)racInit {
    [RACObserve(self, switchStatus) subscribeNext:^(NSNumber*  _Nullable x) {
        [[NSUserDefaults standardUserDefaults] setObject:x.boolValue ? @"1" : @"0" forKey:kPlayOpenDoorSoundKey];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }];
}

#pragma mark - Publish Methods

/// Logout Action
-(void)logout{
    [MobClick profileSignOff];
    // 园区的手动退出登录 不清除密码
    // 4.9.6需求：退出登录的时候清除密码
    [SEGLoginV2Manager segSaveUserPasswordToLocal:@""];
    
    // 退出登录的时候不清除cookie，登录的时候会清除cookie
    [[SEGLoginV2Manager sharedManager] requestLogOutCompleted:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {}];
    [[SEGUhomeMediator sharedInstance] seg_pushToLoginViewController:nil];
}
#pragma mark - Private Methods
-(BOOL)segInitOpenSoundsStatus{
    NSString* string = [[NSUserDefaults standardUserDefaults] objectForKey:kPlayOpenDoorSoundKey];
    BOOL shouldPlaySound = YES;
    if (!string) { //还没有设置 默认为开启
        [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:kPlayOpenDoorSoundKey];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    else {
        if ([string isEqualToString:@"1"]) {
            shouldPlaySound = YES;
        }
        else {
            shouldPlaySound = NO;
        }
    }
    return shouldPlaySound;
}
-(NSArray*)segDataSourceLocal{
//    NSArray *row1 = @[
//        @{@"key":@"1",
//          @"name":@"开门提示语音",
//          @"reuseIdentifier":@"reuserIdentifier_OpenDoor"}];
    
    NSArray *row2 = @[
        @{@"key":@"2",
          @"name":@"修改密码",
          @"reuseIdentifier":@"reuserIdentifier_Other"},
        @{@"key":@"3",
          @"name":@"修改手机号",
          @"reuseIdentifier":@"reuserIdentifier_Other"}];
    
    NSArray *row3 = @[
        @{@"key":@"5",
          @"name":@"关于我们",
          @"reuseIdentifier":@"reuserIdentifier_Other"}];
    
    return @[row2,row3];
}
#pragma mark - Getter
- (NSArray*)dataSource
{
    if (!_dataSource) {
        _dataSource = [self segDataSourceLocal];
    }
    return _dataSource;
}
#pragma mark - Stter
@end
